package com.qdu.bussiness.service.impl;

import java.util.List;

import com.qdu.bussiness.domain.BusEntry;
import com.qdu.common.core.domain.AjaxResult;
import com.qdu.common.utils.DateUtils;
import com.qdu.common.utils.SecurityUtils;
import com.qdu.common.utils.id.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qdu.bussiness.mapper.BusTicketMapper;
import com.qdu.bussiness.domain.BusTicket;
import com.qdu.bussiness.service.IBusTicketService;

/**
 * 门票信息Service业务层处理
 * 
 * @author qdu
 * @date 2022-03-06
 */
@Service
public class BusTicketServiceImpl implements IBusTicketService 
{
    @Autowired
    private BusTicketMapper busTicketMapper;

    /**
     * 查询门票信息
     * 
     * @param ticketId 门票信息主键
     * @return 门票信息
     */
    @Override
    public BusTicket selectBusTicketByTicketId(Long ticketId)
    {
        return busTicketMapper.selectBusTicketByTicketId(ticketId);
    }

    /**
     * 查询门票信息列表
     * 
     * @param busTicket 门票信息
     * @return 门票信息
     */
    @Override
    public List<BusTicket> selectBusTicketList(BusTicket busTicket)
    {
        return busTicketMapper.selectBusTicketList(busTicket);
    }

    /**
     * 新增门票信息
     * 
     * @param busTicket 门票信息
     * @return 结果
     */
    @Override
    public AjaxResult insertBusTicket(BusTicket busTicket)
    {
        // 校验编码重复
        if(checkTicketCodeExist(busTicket.getTicketCode())){
            return AjaxResult.error("当前门票编号已经存在，请更换后重试！");
        }
        busTicket.setTicketId(IdUtil.getId());
        busTicket.setCreateBy(SecurityUtils.getUsername());
        busTicket.setCreateTime(DateUtils.getNowDate());
        int res = busTicketMapper.insertBusTicket(busTicket);
        return res > 0 ? AjaxResult.success(): AjaxResult.error();
    }

    /**
     * 校验门票编号是否重复 - 针对新增
     * @param ticketCode
     * @return
     */
    private boolean checkTicketCodeExist(String ticketCode) {
        return busTicketMapper.checkTicketCodeExist(ticketCode) > 0;
    }

    /**
     * 校验门票编号是否重复 - 针对修改
     * @param ticketCode
     * @return
     */
    private boolean checkTicketCodeExist(String ticketCode, Long ticketId) {
        BusTicket busTicket = busTicketMapper.selectBusTicketByTicketCode(ticketCode);
        if(busTicket == null){
            return false;
        }else if(busTicket.getTicketId().equals(ticketId)){
            return false;
        }
        return true;
    }

    /**
     * 修改门票信息
     * 
     * @param busTicket 门票信息
     * @return 结果
     */
    @Override
    public AjaxResult updateBusTicket(BusTicket busTicket)
    {
        // 校验物料编号是否重复
        if(checkTicketCodeExist(busTicket.getTicketCode(), busTicket.getTicketId())){
            return AjaxResult.error("当前门票编号已经存在，请更换后重试！");
        }
        busTicket.setUpdateBy(SecurityUtils.getUsername());
        busTicket.setUpdateTime(DateUtils.getNowDate());
        int res = busTicketMapper.updateBusTicket(busTicket);
        return res > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除门票信息
     * 
     * @param ticketIds 需要删除的门票信息主键
     * @return 结果
     */
    @Override
    public int deleteBusTicketByTicketIds(Long[] ticketIds)
    {
        return busTicketMapper.deleteBusTicketByTicketIds(ticketIds);
    }

    /**
     * 删除门票信息信息
     * 
     * @param ticketId 门票信息主键
     * @return 结果
     */
    @Override
    public int deleteBusTicketByTicketId(Long ticketId)
    {
        return busTicketMapper.deleteBusTicketByTicketId(ticketId);
    }
}
